//
//  BaseViewController.m
//  B
//
//  Created by lanou on 16/5/6.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController
/**自定义导航栏左按钮*/
- (void)crateBack:(NSString *)name {
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0,0,44,44)];
    [button addTarget:self action:@selector(jumpBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem * spacle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
    spacle.width = -15;
    self.navigationItem.leftBarButtonItems = @[spacle, barButtonItem];
}

- (void)jumpBack:(id)sender {
#pragma mark - 判断当前viewcontroller是push还是present的方式显示的
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        //present方式
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    // 通过present viewcontroller的方式显示的viewcontroller不会存入self.navigationController.viewControllers数组中。而通过push方式显示的viewcontroller会存在该数组的最后
}
// 判断字符串是否为空字符的方法
- (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
@end
